package com.weixin.tool;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpTool {
	
	private static CloseableHttpClient httpClient = HttpClients.createDefault();
	
	public static String doGet(String url){
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response;
		String result = null;
		try {
			response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			if(entity != null){
				result = EntityUtils.toString(entity, "UTF-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static String doPost(String url, String outStr){
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
		HttpResponse response;
		String result = null;
		try {
			response = httpClient.execute(httpPost);
			result = EntityUtils.toString(response.getEntity(),"UTF-8");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}
